import { createApp } from "./main";
export default (context) => {
  return new Promise((resolve, reject) => {
    // 拿出store和router实例
    const { app, router, store } = createApp(context);
    router.push(context.url);
    router.onReady(() => {
      // 获取匹配的路由组件数组
      const matchedComponents = router.getMatchedComponents();

      // 若⽆匹配则抛出异常
      if (!matchedComponents.length) {
        return reject({ code: 404 });
      }

      // 对所有匹配的路由组件调⽤可能存在的`asyncData()`
      Promise.all(
        matchedComponents.map((Component) => {
          if (Component.asyncData) {
            return Component.asyncData({
              store,
              route: router.currentRoute,
            });
          }
        })
      )
        .then(() => {
          // 所有预取钩⼦ resolve 后，
          // store 已经填充⼊渲染应⽤所需状态
          // 将状态附加到上下⽂，且 `template` 选项⽤于 renderer 时，
          // 状态将⾃动序列化为 `window.__INITIAL_STATE__`，并注⼊ HTML
          context.state = store.state;

          resolve(app);
        })
        .catch(reject);
    }, reject);
  });
};
